package client.adminGUI;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Container;

import client.adminGUI.login.LoginPanel;
import client.adminGUI.user.UserPanel;
import client.adminGUI.UsersPanel;
import client.backend.Connection;

import java.sql.SQLException;
import server.user.BadUserException;

public class GUIAdmin extends JFrame {
	private Container pane = this.getContentPane();
	private static GUIAdmin instance = null;
	private JPanel topPanel = new JPanel();
	private BottomPanel bottomPanel = new BottomPanel();
	private LoginPanel loginPanel = LoginPanel.getInstance();
	private UserPanel userPanel;
	
	public static final int WIDTH = 1024;
	public static final int HEIGHT = 768;
	public static final Color Background_Color = Color.black;
    public static final Color Text_Color = Color.white;
	
	/**
	 * This is the default constructor
	 */
	public GUIAdmin() {
		super();
		this.setSize(WIDTH, HEIGHT);
		this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
		this.setMinimumSize(new Dimension(WIDTH, HEIGHT));
		this.setMaximumSize(new Dimension(WIDTH, HEIGHT));
		this.setResizable(false);
		this.setTitle("Automated Currency Exchange: Administration");
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setLayout(null);
		
		topPanel.setLayout(null);
		topPanel.setBounds(0, 0, WIDTH, HEIGHT-BottomPanel.HEIGHT);
		topPanel.setBackground(Background_Color);
		topPanel.add(loginPanel);
		
		loginPanel.setVisible(false);
		bottomPanel.setVisible(true);
		
		this.add(topPanel);
		this.add(bottomPanel);
		
		displayLogin();
	}
	
	/**
	 * Show the login panel
	 */
	public void displayLogin() {
		loginPanel.setVisible(true);
	}
	
	/**
	 * Hide the login panel
	 */
	public void hideLogin() {
		loginPanel.setVisible(false);
	}
	
	/**
	 * Display the users panel
	 */
	public void displayUsers(){
		UsersPanel.getInstance().setVisible(true);
	}
	
	/**
	 * Hide the users panel
	 */
	public void hideUsers(){
		UsersPanel.getInstance().setVisible(false);
	}
	
	/**
	 * Create a new panel for a specified user
	 * @param userid	User ID
	 */
	public void showUser(int userid){
		hideUsers();
		userPanel = new UserPanel(userid);
		topPanel.add(userPanel);
	}
	
	/**
	 * Remove the user panel and show main screen
	 */
	public void hideUser(){
		this.remove(userPanel);
		displayUsers();
	}
	
	/**
	 * Show main screen after login
	 */
	public void loggedIn(){
		hideLogin();
		topPanel.add(UsersPanel.getInstance());
		displayUsers();
	}
	
	/**
	 * Set the GUIAdmin panel message
	 * @param text	New Message
	 */
	public void setMessage(String text){
		bottomPanel.setMessage(text);
	}
	
	/**
	 * Clear GUIAdmin panel message
	 */
	public void clearMessage(){
		bottomPanel.setMessage("");
	}
	
	/**
	 * GUIAdmin getter
	 * @return	Only GUIAdmin
	 */
	public static GUIAdmin getInstance() {
		if (instance == null) {
			instance = new GUIAdmin();
		}
		return instance;
	}
}

